package com.api.signature.auto.strategy;

import com.api.signature.auto.annotation.ApiSignature;
import com.api.signature.auto.function.ApiGetAnnotationFunction;
import com.api.signature.auto.function.ConsumerPlus;
import com.signature.auto.common.servlet.ApiRequestForServlet;
import com.api.signature.auto.spring.ApiSignContext;
import jakarta.servlet.http.HttpServletRequest;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;

/**
 * @author Jerry
 * @date 2024-01-24 10:18
 */
public final class ApiSignatureStrategy {


    private ApiSignatureStrategy() {
    }

    /**
     * 获取 ApiSignatureStrategy 对象的单例引用
     */
    public static final ApiSignatureStrategy INSTANCE = new ApiSignatureStrategy();

    // ----------------------- 所有策略

    public ConsumerPlus<Method, HttpServletRequest> checkMethodAnnotation = (method, request) ->
            INSTANCE.checkElementAnnotation.accept(method, request);


    /**
     * 注解规则校验
     */
    public ConsumerPlus<AnnotatedElement, HttpServletRequest> checkElementAnnotation = (target, request) -> {
        // 校验 @ApiSignature 注解
        ApiSignature apiSignature = (ApiSignature) INSTANCE.getAnnotation.apply(target, ApiSignature.class);
        if (apiSignature != null) {
            ApiSignContext.getApiSignService().checkRequest(new ApiRequestForServlet(request));
        }

    };

    /**
     * 从元素上获取注解
     */
    public ApiGetAnnotationFunction getAnnotation = AnnotatedElement::getAnnotation;
}
